<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends MX_Controller {

	function __construct(){
		parent::__construct();
		$this->template->write('title','');
		$this->template->write('meta_keywords','');
		$this->template->write('meta_description','');
		$this->template->write('meta_image','');
		$this->template->write('meta_video','');
	}

	/*-------------------------------------- FrontEnd --------------------------------------*/
	function index(){
		$data = array();
		$this->load->model('home_model','home_model');

		//about us
		$abouts = $this->home_model->get_abouts();
		$data['abouts'] = $abouts;


		$result_poll = $this->home_model->get_poll();
		//pr($result_poll , 1);
		$data['poll'] = $result_poll;
		if(isset($result_poll->id) && $result_poll->id > 0){
			$result_poll_answer = $this->home_model->get_poll_answer($result_poll->id);
			$data['poll_answer'] = $result_poll_answer;
		}

		$research_id = 17;
		$data['researchs'] = $this->home_model->getNews($research_id);

		$this->template->write('title',lang('sitename'));
		$this->template->write_view('content','FRONTEND/index',$data);
		$this->template->render();
	}

	function reCaptcha(){
		//create captcha
		$this->load->helper('captcha');
		$expiration = time()-7200; // Two hour limit
		$img_path = BASEFOLDER.'static/images/captcha/';
		$img_url = PATH_URL.'static/images/captcha/';
		$vals = array(
			'img_path' =>  $img_path,
			'img_url' => $img_url,
			'word'	  => random_string(5),
			'img_width'	 => '83',
    		'img_height' => '33',
			);

		$cap = create_captcha($vals);
		$data = array(
			'captcha_time'	=> $cap['time'],
			'ip_address'	=> getIP(),
			'word'	 => $cap['word']
		);
		$query = $this->db->insert_string(CAPTCHA_TB, $data);
		$this->db->query($query);
		$this->session->set_userdata('captcha', strtolower($cap['word']));
		$this->db->query("DELETE FROM ".CAPTCHA_TB." WHERE captcha_time < ".$expiration);
		echo $cap['image'];
	}
	function verifyCaptcha(){
		$captcha = $this->input->post('inp_captcha');
		if ($captcha == $this->session->userdata('captcha') || strtolower($captcha) == strtolower($this->session->userdata('captcha')))
		{
			echo 'true';
		}
		else{
			echo 'false';
		}
	}
	
	function website_map()
	{
		$data = array();
		$this->template->write('title','SGL - Website map');
		$this->template->write_view('content','FRONTEND/webmap',$data);
		$this->template->render();
	}
	
	function search_google()
	{
		
		$search_words = $this->input->post('keyword');
		if (!empty($search_words)) {
		
			$search_words = urldecode($search_words);
			$redirect_url = PATH_URL.$this->current_lang.'/tim-kiem?keyword='.$search_words;
			redirect($redirect_url);
		}
		$data = array();
		$this->template->write('title','SGL');
		$this->template->write_view('content','FRONTEND/search_google',$data);
		$this->template->render();
	}

	function backup(){
		// Load the DB utility class
		$this->load->dbutil();

		// Backup your entire database and assign it to a variable
		$prefs = array(
                'tables'      => array('table1', 'table2'),  // Array of tables to backup.
                'ignore'      => array(),           // List of tables to omit from the backup
                'format'      => 'txt',             // gzip, zip, txt
                'filename'    => 'mybackup.sql',    // File name - NEEDED ONLY WITH ZIP FILES
                'add_drop'    => TRUE,              // Whether to add DROP TABLE statements to backup file
                'add_insert'  => TRUE,              // Whether to add INSERT data to backup file
                'newline'     => "\n"               // Newline character used in backup file
              );

		$backup = $this->dbutil->backup();

		// Load the file helper and write the file to your server
		$this->load->helper('file');
		$bkname = DB_NAME .'_'. date('d-m-Y') . '.gz';
		//pr($bkname,1);
		write_file('/database/' . $bkname, $backup);


		// Load the download helper and send the file to your desktop
		$this->load->helper('download');
		force_download($bkname, $backup);
	}

}
